package org.hutb.community.entity;

/**
 * @ClassName Page
 * @Description 分页组件 封装分页相关的信息
 * @Author xubo
 * @Date 2021/11/25 1:02
 */
public class Page{
    private int current = 1;
    private int limit = 10;
    private int rows;
    private String path;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current >= 1){
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit>=1 && limit<=30){
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if (rows >= 0){
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getOffset(){
        return (current - 1)*limit;
    }

    //计算数据库中的帖子数量一共可以分为多少页.
    public int getTotal(){
        if (rows % limit == 0){
            return rows/limit;
        }else {
            return rows / limit + 1;
        }
    }

    //获取起始页码
    public int getFrom(){
        int from = current - 2;
        return from <=1 ? 1: from;
    }
    //获取终止页码
    public int getTo(){
        int to = current +2;
        int total = getTotal();
        return to > total ? total: to;
    }

    @Override
    public String toString() {
        return "Page{" +
                "current=" + current +
                ", limit=" + limit +
                ", rows=" + rows +
                ", path='" + path + '\'' +
                '}';
    }
}
